package org.wendu.wdoa.security.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.wendu.wdoa.common.Result;
import org.wendu.wdoa.common.page.Page;
import org.wendu.wdoa.security.dto.UserQueryDto;
import org.wendu.wdoa.security.service.UserService;
import org.wendu.wdoa.security.model.Role;
import org.wendu.wdoa.security.model.User;

import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /*
        Query String（查询字符串方式，url后通过？引出的参数） 适用于任何请求
        请求体（payload方式json请求体）适用post、delete、put等请求


     */
    @GetMapping("")
    public Result<Page<User>> userList(UserQueryDto dto){
         Page<User> page = userService.getUserPage(dto);
         return Result.OK(page);
    }


    @PutMapping("/role")
    public Result setUserRoles(
            @NotNull(message = "用户账号不得为空！") String u_id,
            @NotNull(message = "角色编号不得为空！") @RequestBody Integer[] roleIds){

        userService.setUserRoles(u_id,roleIds);
        return Result.OK();

    }

    @GetMapping("/role/{userId}")
    public Result<List<Role>> userRoleList(@PathVariable String userId){
        List<Role> roleList = userService.getUserRoleList(userId);
        return Result.OK(roleList);
    }


}
